################################################################################
##
## Purpose: This script creates Figure 7, along with SI figure 11 and SI tables
##          6 and 7.
##
## Author: James Bisbee (james.h.bisbee@vanderbilt.edu)
##
## Input Files:
##  - ./data/prepped/finalData.RData: Prepped data from 9_DATA_final_build.R
##
## Output Files:
##  - ./output/figures/MS_figure_7.pdf
##  - ./output/tables/SI_table_6.tex
##  - ./output/tables/SI_table_7.tex
##  - ./output/figures/SI_figure_11.pdf
##
##
## See associated log file for compute environment, package versions, 
##  and date of most recent run.
##
################################################################################
rm(list = ls())
gc()
require(tidyverse)
require(ggridges)
require(fixest)

set.seed(123)

# Compute details
print(paste0('Compute environment from ',Sys.Date(),' run by Bisbee'))
if(Sys.info()['sysname'] == 'Windows') {
  ram_size = system("wmic MemoryChip get Capacity", intern = TRUE)[-1]
  model_name = system("wmic cpu get name", intern = TRUE)[2] # nocov
  vendor_id = system("wmic cpu get manufacturer", intern = TRUE)[2] # nocov
  
  print(list(ram = stringr::str_squish(ram_size)[1],
             vendor_id = stringr::str_squish(vendor_id),
             model_name = stringr::str_squish(model_name),
             no_of_cores = parallel::detectCores()))
} else if(Sys.info()['sysname'] == 'Linuxs') {
  splitted <- strsplit(system("ps -C rsession -o %cpu,%mem,pid,cmd", intern = TRUE), " ")
  df <- do.call(rbind, lapply(splitted[-1], 
                              function(x) data.frame(
                                cpu = as.numeric(x[2]),
                                mem = as.numeric(x[4]),
                                pid = as.numeric(x[5]),
                                cmd = paste(x[-c(1:5)], collapse = " "))))
  df
} else {
  cat("If not on Linux or Windows, you'll have to figure out your own solution to seeing the compute environment.")
}

sessionInfo()



load('./data/prepped/finalData.RData')

# Data prep
thresholds <- utterance_level %>%
  filter(grepl('FED',opensecretsID)) %>%
  group_by(docID,chamber) %>%
  summarise(n=n(),
            Greenspan = grepl('GREENSPAN',opensecretsID),
            Yellen = grepl('YELLEN',opensecretsID),
            Bernanke = grepl('BERNANKE',opensecretsID),
            Powell = grepl('POWELL',opensecretsID)) %>%
  distinct() %>%
  ungroup() %>%
  data.frame()


toAnalDyad <- speaker_level %>%
  mutate(respondingTo = relevel(factor(respondingTo),ref = 'FEDBERNANKE'))


BernYellRes <- list()
dates <- list(as.Date(c('2006-01-01','2018-01-01')),
              as.Date(c('2008-01-01','2016-01-01')),
              as.Date(c('2013-01-01','2015-01-01')))

for(ds in dates) {
  for(cham in c('House','Senate')) {
    # stop()
    BernYellRes[[paste(ds,collapse = ' to ')]][[cham]] <- feols(as.formula(paste0('interruptorPct ~ factor(respondingTo)',
                                                                                 '| opensecretsID')),
                                                               toAnalDyad %>%
                                                                 filter(date > as.Date(ds[1]) & date < as.Date(ds[2]),
                                                                        chamber == cham) %>%
                                                                 filter(grepl('FED',respondingTo),
                                                                        !grepl('FED',opensecretsID)) %>%
                                                                 group_by(opensecretsID) %>%
                                                                 mutate(n = n(),
                                                                        keep = any(grepl('YELLEN',respondingTo)) & any(grepl('BERNANKE',respondingTo))) %>%
                                                                 ungroup() %>%
                                                                 filter(n > 1,
                                                                        keep)) 
  }
}

toAnalDyad <- toAnalDyad %>%
  mutate(respondingTo = relevel(factor(respondingTo),ref = 'FEDYELLEN'))
YellPowRes <- list()
dates <- list(as.Date(c('2014-01-01','2021-01-01')),
              as.Date(c('2016-01-01','2020-01-01')),
              as.Date(c('2017-01-01','2019-01-01')))
for(ds in dates) {
  for(cham in c('House','Senate')) {
    YellPowRes[[paste(ds,collapse = ' to ')]][[cham]] <- feols(as.formula(paste0('interruptorPct ~ factor(respondingTo)',
                                                                                '| opensecretsID')),
                                                              toAnalDyad %>%
                                                                filter(date > as.Date(ds[1]) & date < as.Date(ds[2]),
                                                                       chamber == cham) %>%
                                                                filter(grepl('FED',respondingTo),
                                                                       !grepl('FED',opensecretsID)) %>%
                                                                group_by(opensecretsID) %>%
                                                                mutate(n = n(),
                                                                       keep = any(grepl('YELLEN',respondingTo)) & any(grepl('POWELL',respondingTo))) %>%
                                                                ungroup() %>%
                                                                filter(n > 1,
                                                                       keep)) 
  }
}



toplot <- NULL
for(i in 1:length(YellPowRes)) {
  toplot <- bind_rows(toplot,as_tibble(data.frame(summary(YellPowRes[[i]]$House)$coeftable) %>%
                                         rename_all(.funs = function(x) gsub('Estimate','est',gsub('Std..Error','se',gsub('t.value','tstat',gsub('Pr...t..','pval',x))))) %>%
                                         mutate(ref = rownames(.))) %>%
                        mutate(period = names(YellPowRes)[i],
                               chamber = 'House',
                               chairComp = 'Powell'))
  toplot <- bind_rows(toplot,as_tibble(data.frame(summary(YellPowRes[[i]]$Senate)$coeftable) %>%
                                         rename_all(.funs = function(x) gsub('Estimate','est',gsub('Std..Error','se',gsub('t.value','tstat',gsub('Pr...t..','pval',x))))) %>%
                                         mutate(ref = rownames(.))) %>%
                        mutate(period = names(YellPowRes)[i],
                               chamber = 'Senate',
                               chairComp = 'Powell'))
}
for(i in 1:length(BernYellRes)) {
  toplot <- bind_rows(toplot,as_tibble(data.frame(summary(BernYellRes[[i]]$House)$coeftable) %>%
                                         rename_all(.funs = function(x) gsub('Estimate','est',gsub('Std..Error','se',gsub('t.value','tstat',gsub('Pr...t..','pval',x))))) %>%
                                         mutate(ref = rownames(.))) %>%
                        mutate(period = names(BernYellRes)[i],
                               chamber = 'House',
                               chairComp = 'Bernanke'))
  toplot <- bind_rows(toplot,as_tibble(data.frame(summary(BernYellRes[[i]]$Senate)$coeftable) %>%
                                         rename_all(.funs = function(x) gsub('Estimate','est',gsub('Std..Error','se',gsub('t.value','tstat',gsub('Pr...t..','pval',x))))) %>%
                                         mutate(ref = rownames(.))) %>%
                        mutate(period = names(BernYellRes)[i],
                               chamber = 'Senate',
                               chairComp = 'Bernanke'))
}

pdf('./output/figures/MS_figure_7.pdf',width = 8,height = 5)
toplot %>%
  mutate(ref = gsub('factor.*?FED','',ref)) %>%
  filter((chairComp == 'Powell' & ref == 'POWELL') | (chairComp == 'Bernanke' & ref == 'YELLEN')) %>%
  mutate(period = gsub('-01-01','',gsub('to','to\n',period)),
         chairComp = ifelse(chairComp == 'Bernanke','Bernanke -> Yellen','Yellen -> Powell')) %>%
  ggplot(aes(x = period,y = est,color = chamber,shape = chamber)) + 
  geom_hline(yintercept = 0,linetype = 'dashed') + 
  scale_shape_manual(name = 'Chamber',values = c(21,22)) + 
  geom_errorbar(aes(ymin = est - 2*se,ymax = est + 2*se),position = position_dodge(width = .2),width = 0) + 
  geom_point(position = position_dodge(width = .2),fill = 'white',size = 3) + 
  facet_grid(~chairComp,scales = 'free') + 
  theme_ridges() + 
  scale_color_manual(name = 'Chamber',values = c('grey30','grey60')) + 
  theme(strip.background = element_rect(fill = 'white',size = .5,linetype = 'solid')) + 
  xlab('Comparison Window') + ylab('Change in interruptions\nassociated with change in Fed chair')
dev.off()



# Tables
mList <- list()
for(chamb in c('House')) { 
  for(d in names(BernYellRes)) {
    mList[[paste0(chamb,': ',d)]] <- BernYellRes[[d]][[chamb]]
  }
  for(d in names(YellPowRes)) {
    mList[[paste0(chamb,': ',d)]] <- YellPowRes[[d]][[chamb]]
  }
}

dict <- c('factor(respondingTo)FEDYELLEN' = 'Yellen (ref. Bernanke)',
             'factor(respondingTo)FEDPOWELL' = 'Powell (ref. Yellen)',
          'opensecretsID' = 'Speaker')
etable(mList,dict = dict,depvar = F,digits = 3,
       signif.code = c('***' = .001,'**' = .01,'*' = .05,'\\dag' = .1),
       replace = T,
       file = './output/tables/SI_table_6.tex')


mList <- list()
for(chamb in c('Senate')) { 
  for(d in names(BernYellRes)) {
    mList[[paste0(chamb,': ',d)]] <- BernYellRes[[d]][[chamb]]
  }
  for(d in names(YellPowRes)) {
    mList[[paste0(chamb,': ',d)]] <- YellPowRes[[d]][[chamb]]
  }
}

etable(mList,dict = dict,depvar = F,digits = 3,
       signif.code = c('***' = .001,'**' = .01,'*' = .05,'\\dag' = .1),
       replace = T,
       file = './output/tables/SI_table_7.tex')


# RR1: placebo comparing first terms of Bernanke, and first term of powell relative to Bernanke
GreenBernRes <- list()


dates <- list(as.Date(c('2001-01-01','2014-01-01')),
              as.Date(c('2002-01-01','2010-01-01')),
              as.Date(c('2005-01-01','2007-01-01')))

for(ds in dates) {
  for(cham in c('House','Senate')) {
    # stop()
    GreenBernRes[[paste(ds,collapse = ' to ')]][[cham]] <- feols(as.formula(paste0('interruptorPct ~ factor(respondingTo)',
                                                                                  '| opensecretsID')),
                                                                toAnalDyad %>%
                                                                  filter(date > as.Date(ds[1]) & date < as.Date(ds[2]),
                                                                         chamber == cham) %>%
                                                                  filter(grepl('FED',respondingTo),
                                                                         !grepl('FED',opensecretsID)) %>%
                                                                  group_by(opensecretsID) %>%
                                                                  mutate(n = n(),
                                                                         keep = any(grepl('GREENSPAN',respondingTo)) & any(grepl('BERNANKE',respondingTo))) %>%
                                                                  ungroup() %>%
                                                                  filter(n > 1,
                                                                         keep)) 
  }
}


BernPowRes <- list()
dates <- list(as.Date(c('2010-01-01','2022-01-01')),
              as.Date(c('2012-01-01','2020-01-01')),
              as.Date(c('2013-01-01','2019-01-01')))

for(ds in dates) {
  for(cham in c('House','Senate')) {
    # stop()
    BernPowRes[[paste(ds,collapse = ' to ')]][[cham]] <- feols(as.formula(paste0('interruptorPct ~ factor(respondingTo)',
                                                                                   '| opensecretsID')),
                                                                 toAnalDyad %>%
                                                                   filter(date > as.Date(ds[1]) & date < as.Date(ds[2]),
                                                                          chamber == cham) %>%
                                                                   filter(grepl('FED',respondingTo),
                                                                          !grepl('FED',opensecretsID),
                                                                          !yellenTime) %>%
                                                                   group_by(opensecretsID) %>%
                                                                   mutate(n = n(),
                                                                          keep = any(grepl('POWELL',respondingTo)) & any(grepl('BERNANKE',respondingTo))) %>%
                                                                   ungroup() %>%
                                                                   filter(n > 1,
                                                                          keep)) 
  }
}


toplot <- NULL
for(i in 1:length(GreenBernRes)) {
  toplot <- bind_rows(toplot,as_tibble(data.frame(summary(GreenBernRes[[i]]$House)$coeftable) %>%
                                         rename_all(.funs = function(x) gsub('Estimate','est',gsub('Std..Error','se',gsub('t.value','tstat',gsub('Pr...t..','pval',x))))) %>%
                                         mutate(ref = rownames(.))) %>%
                        mutate(period = names(GreenBernRes)[i],
                               chamber = 'House',
                               chairComp = 'Greenspan vs. Bernanke'))
  toplot <- bind_rows(toplot,as_tibble(data.frame(summary(GreenBernRes[[i]]$Senate)$coeftable) %>%
                                         rename_all(.funs = function(x) gsub('Estimate','est',gsub('Std..Error','se',gsub('t.value','tstat',gsub('Pr...t..','pval',x))))) %>%
                                         mutate(ref = rownames(.))) %>%
                        mutate(period = names(GreenBernRes)[i],
                               chamber = 'Senate',
                               chairComp = 'Greenspan vs. Bernanke'))
}
for(i in 1:length(BernPowRes)) {
  toplot <- bind_rows(toplot,as_tibble(data.frame(summary(BernPowRes[[i]]$House)$coeftable) %>%
                                         rename_all(.funs = function(x) gsub('Estimate','est',gsub('Std..Error','se',gsub('t.value','tstat',gsub('Pr...t..','pval',x))))) %>%
                                         mutate(ref = rownames(.))) %>%
                        mutate(period = names(BernPowRes)[i],
                               chamber = 'House',
                               chairComp = 'Bernanke vs. Powell'))
  toplot <- bind_rows(toplot,as_tibble(data.frame(summary(BernPowRes[[i]]$Senate)$coeftable) %>%
                                         rename_all(.funs = function(x) gsub('Estimate','est',gsub('Std..Error','se',gsub('t.value','tstat',gsub('Pr...t..','pval',x))))) %>%
                                         mutate(ref = rownames(.))) %>%
                        mutate(period = names(BernPowRes)[i],
                               chamber = 'Senate',
                               chairComp = 'Bernanke vs. Powell'))
}

pdf('./output/figures/SI_figure_11.pdf',width = 8,height = 5)
toplot %>%
  mutate(ref = gsub('factor.*?FED','',ref)) %>%
  mutate(chairComp = factor(gsub(' vs. ','->',chairComp),
                            levels = c('Greenspan->Bernanke','Bernanke->Powell'))) %>%
  mutate(period = gsub('-01-01','',gsub('to','to\n',period))) %>%
  ggplot(aes(x = period,y = est,color = chamber,shape = chamber)) + 
  geom_hline(yintercept = 0,linetype = 'dashed') + 
  scale_shape_manual(name = 'Chamber',values = c(21,22)) + 
  geom_errorbar(aes(ymin = est - 2*se,ymax = est + 2*se),position = position_dodge(width = .2),width = 0) + 
  geom_point(position = position_dodge(width = .2),fill = 'white',size = 3) + 
  facet_grid(~chairComp,scales = 'free') + 
  theme_ridges() + 
  scale_color_manual(name = 'Chamber',values = c('grey30','grey60')) + 
  theme(strip.background = element_rect(fill = 'white',size = .5,linetype = 'solid')) + 
  xlab('Comparison Window') + ylab('Change in interruptions\nassociated with change in Fed chair')
dev.off()

# EOF